package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Todo;
import com.itheima.service.TodoService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/todo")
public class TodoController {

    @Autowired
    private TodoService todoService;

    /**
     * 增加待办事项
     * @param todoVO
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Map todoVO){
        todoService.add(todoVO);
        return new Result("success","插入数据成功");
    }

    /**
     * 删除待办事项
     * @param todoVO
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Map todoVO){
        todoService.delete(todoVO);
        return new Result("success","删除成功");
    }

    /**
     * 编辑待办事项
     * @param todoVO
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Map todoVO){
        todoService.edit(todoVO);
        return new Result("success","编辑成功");
    }

    /**
     * 查询待办事项
     * @param todoVO
     * @return
     */
    @PostMapping("/getList")
    public Result getList(@RequestBody Map todoVO){
        Page<Todo> pageList = todoService.getList(todoVO);
        PageResult<Todo> pageResult = new PageResult<>();
        pageResult.setRecords(pageList.getResult());
        pageResult.setCurrent((long) pageList.getPageNum()+1);
        pageResult.setOptimizeCountSql(true);
        pageResult.setSize((long) pageList.getPageSize());
        pageResult.setTotal(pageList.getTotal());
        pageResult.setSearchCount(true);
        return new Result("success",pageResult);
    }
}
